#coding=utf-8
'''
Created on 2013-9-3

@author: Ben

武将数据
'''

import GeneralVO

#是否刷新武将数据
refreshGenerals = False
#武将列表
generalList = {}
#募兵令数量
token = 0

def init(data):
    global token
    global generalList
    if "token" in data:
        token = data["token"]
    if "military" in data:
        for general in data["military"]:
            generalVO = GeneralVO.GeneralVO(general)
            generalList[generalVO.generalId] = generalVO

'''
更新武将数据
'''
def update(data):
    global generalList
    if "general" in data and "military" in data["general"]:
        for general in data["general"]["military"]:
            if "generalId" in general:
                generalId = int(general["generalId"])
                if generalId in generalList:
                    generalVO = generalList[generalId]
                    if generalVO != None:
                        generalVO.update(general)
            
'''
获取等级最低的武将
'''
def getWeakestGeneralId():
    global generalList
    weakestGeneralLv = 1000
    weakestGeneralId = 0
    for generalId in generalList:
        general = generalList[generalId]
        if general.generalLv < weakestGeneralLv:
            weakestGeneralLv = general.generalLv
            weakestGeneralId = general.generalId
    return weakestGeneralId

'''
根据武将名的中文拼音获取武将id
'''
def getGeneralIdByPic(pic):
    global generalList
    targetGeneralId = 0
    for generalId in generalList:
        general = generalList[generalId]
        if general.pic == pic:
            targetGeneralId = generalId
            break
    #如果没有该武将
    if targetGeneralId <= 0:
        targetGeneralId = getWeakestGeneralId()
    return targetGeneralId

'''
获取武将数据
'''
def getGeneralVO(generalId):
    global generalList
    if generalId in generalList:
        return generalList[generalId]
    else:
        return 0
    
'''
清理
'''
def clear():
    global refreshGenerals
    refreshGenerals = False